#!/usr/bin/env python3

word_groups = (
	(
		('家园', '毕业', '裁缝', '需要'),
		('家园.jpg', '毕业.jpg', '裁缝.jpg', '需要.jpg'),
	),
	(
		('耕', '织', '箱', '瓶'),
		('耕.jpg', '织.jpg', '箱.jpg', '瓶.jpg')
	),
	(
		('罐', '舞台', '绿伞', '摇篮'),
		('罐.jpg', '舞台.jpg', '绿伞.jpg', '摇篮.jpg')
	),
	(
		('眼镜', '测量', '宣布', '装'),
		('眼镜.jpg', '测量.jpg', '宣布.jpg', '装.jpg')
	),
	(
		('状', '迈', '缝', '恐'),
		('状.jpg', '迈.jpg', '缝.jpg', '恐.jpg')
	),
	(
		('迟', '归', '提', '扇'),
		('迟.jpg', '归.jpg', '提.jpg', '扇.jpg')
	),
	(
		('抓', '拖', '运', '弄'),
		('抓.jpg', '拖.jpg', '运.jpg', '弄.jpg')
	),
	(
		('已', '凉', '查', '藏'),
		('已.jpg', '凉.jpg', '查.jpg', '藏.jpg')
	),
	(
		('捏', '融', '鸳鸯', '追'),
		('捏.jpg', '融.jpg', '鸳鸯.jpg', '追.jpg')
	),
	(
		('奖', '狗熊', '敲', '湖'),
		('奖.jpg', '狗熊.jpg', '敲.jpg', '湖.jpg')
	),
	(
		('盛', '夜晚', '美丽', '方向'),
		('盛.jpg', '夜晚.jpg', '美丽.jpg', '方向.jpg')
	),
	(
		('光', '橡皮', '丢', '蜡笔'),
		('光.jpg', '橡皮.jpg', '丢.jpg', '蜡笔.jpg')
	),
	(
		('挂', '秘密', '清凉', '镶嵌'),
		('挂.jpg', '秘密.jpg', '清凉.jpg', '镶嵌.jpg')
	),
	(
		('悠扬', '沉浸', '泉眼', '荷'),
		('悠扬.jpg', '沉浸.jpg', '泉眼.jpg', '荷.jpg')
	),
	(
		('蜻蜓', '独自', '登', '放哨'),
		('蜻蜓.jpg', '独自.jpg', '登.jpg', '放哨.jpg')
	),
	(
		('口袋', '灯笼', '惊动', '海马'),
		('口袋.jpg', '灯笼.jpg', '惊动.jpg', '海马.jpg')
	),
	(
		('龙虾', '船帆', '慈', '蓝天'),
		('龙虾.jpg', '船帆.jpg', '慈.jpg', '蓝天.jpg')
	),
	(
		('骑', '草原', '羊羔', '脊背'),
		('骑.jpg', '草原.jpg', '羊羔.jpg', '脊背.jpg')
	),
)
deck_pfx = 'All::1_Life::育儿::徐韬略::幼儿园::大班下学期::汉字::子集'
deck_idx_start = 0

#need_gen_audios = True
need_gen_audios = False
output_dir = r'output'
anki_import_data_file_name = 'anki_import_data.txt'

from lib.anki import generate_anki_import_file
from lib.my_tts import gen_audio_by_bytedance

import os
import random

anki_media_dir = r'C:\Users\XYQTJ\AppData\Roaming\Anki2\User 1\collection.media'

def genRndHexStr():
    random_string = secrets.token_hex(16)
    hash_object = hashlib.sha256()
    hash_object.update(random_string.encode('utf-8'))
    hex_dig = hash_object.hexdigest()
    return hex_dig

def genAudioFileName(name):
    return name + '.mp3'

def genAudios(texts, output_dir):
    for text in texts:
        audio_path = os.path.join(output_dir, genAudioFileName(text))
        gen_audio_by_bytedance(text, audio_path)

def genAudiosByWords(en_words, cn_words, output_dir):
    isConflict = False
    for item in en_words:
        audio_name = genAudioFileName(item)
        audio_path = os.path.join(anki_media_dir, audio_name)
        if os.path.exists(audio_path):
            print(audio_path + " already exist")
            isConflict = True        

    for item in cn_words:
        audio_name = genAudioFileName(item)
        audio_path = os.path.join(anki_media_dir, audio_name)
        if os.path.exists(audio_path):
            print(audio_path + " already exist")
            isConflict = True        

    #print(genRndHexStr())
    #if isConflict:
    #    return False

    genAudios(en_words, output_dir)
    genAudios(cn_words, output_dir)
    return True
        
def genAnkiImportData(output_dir, word_groups):
    grp_idx = 1
    for group in word_groups:
        cards = []
        word_cnt = len(group[0])
        deck_name = deck_pfx + str(deck_idx_start + grp_idx).zfill(2)
        import_file_path = output_dir + '/anki_import_data' + str(grp_idx) + '.txt'
        grp_idx += 1
        # print('word count is ' + str(word_cnt))
        words = group[0]
        imgs = group[1]
        for index in range(word_cnt):
            word = words[index]
            img = imgs[index]
            
            # recognize
            card1 = {}
            card1['front'] = "<h1>" + word + "</h1>"
            card1['back'] = "<img src=\"" + img + "\"><br>[sound:" + word +".mp3]"
            cards.append(card1)
            
            # listen
            card2 = {}
            card2['front'] = "<img src=\"" + img + "\"><br>[sound:" + word +".mp3]" \
                + "<h1>1 " + words[0] + "</h1>" \
                + "<h1>2 " + words[1] + "</h1>" \
                + "<h1>3 " + words[2] + "</h1>" \
                + "<h1>4 " + words[3] + "</h1>"
            card2['back'] = "<h1>" + word + "</h1>"
            cards.append(card2)
        generate_anki_import_file(cards, deck_name, import_file_path)


# create output directory
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

anki_import_data_file_path = os.path.join(output_dir, anki_import_data_file_name)
# genAnkiImportData(anki_import_data_file_path, en_words, cn_words, imgs)
genAnkiImportData(output_dir, word_groups)
if need_gen_audios:
    # genAudiosByWords(en_words, cn_words, output_dir)
    for grp in word_groups:
        word_cnt = len(grp[0])
        words = grp[0]
        for index in range(word_cnt):
            word = words[index]
            audio_name = word + '.mp3'
            audio_path = os.path.join(output_dir, audio_name)
            gen_audio_by_bytedance(word, audio_path)